#include "database.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>    // mkdir()
#include <sys/types.h>   // 某些系统可能需要
#include <unistd.h>



void print_menu(void) {
    printf("\nSQLite Demo Menu:\n");
    printf("1. Insert user\n");
    printf("2. Update user\n");
    printf("3. Delete user\n");
    printf("4. Get user\n");
    printf("5. List all users\n");
    printf("6. Execute transaction\n");
    printf("7. Search users\n");
    printf("0. Exit\n");
    printf("Choice: ");
}

int mkdirs(const char *path, mode_t mode) {
    char tmp[256];
    char *p = NULL;
    size_t len;
    
    snprintf(tmp, sizeof(tmp), "%s", path);
    len = strlen(tmp);
    if(tmp[len - 1] == '/')
        tmp[len - 1] = 0;
        
    for(p = tmp + 1; *p; p++) {
        if(*p == '/') {
            *p = 0;
            mkdir(tmp, mode);
            *p = '/';
        }
    }
    return mkdir(tmp, mode);
}

void split_path(const char *full_path, char *dir_path, char *filename)
{
    const char *last_slash = strrchr(full_path, '/');

    if (last_slash)
    {
        // 复制目录部分
        int dir_len = last_slash - full_path;
        strncpy(dir_path, full_path, dir_len);
        dir_path[dir_len] = '\0';

        // 复制文件名部分
        strcpy(filename, last_slash + 1);
    }
    else
    {
        // 没有斜杠，则整个字符串都是文件名
        strcpy(dir_path, "");
        strcpy(filename, full_path);
    }
}

int main(void) {
    // mkdir("test/download", 0755);
    char path[100] = "./test/download/test.db";

    char user_db_dir[256] = {0};
    char user_db_file[256] = {0};



    split_path(path, user_db_dir, user_db_file);

    printf("Directory: %s\n", user_db_dir);  // 输出: ./test/download
    printf("Filename: %s\n", user_db_file);  // 输出: test.db

    if (access(user_db_dir, F_OK) == -1) {
        printf("-------path:%s-------\n", user_db_dir);
        mkdirs(user_db_dir, 0755);
    }

    if (!db_init(path)) {
        return 1;
    }

    if (!db_create_tables()) {
        db_close();
        return 1;
    }

    int choice;
    char name[50], email[100], search_term[50];
    int id, age;
    User user;

    do {
        print_menu();
        scanf("%d", &choice);
        getchar(); // 消费换行符

        switch (choice) {
            case 1:
                printf("Enter name: ");
                fgets(name, sizeof(name), stdin);
                name[strcspn(name, "\n")] = 0;
                
                printf("Enter age: ");
                scanf("%d", &age);
                getchar();
                
                printf("Enter email: ");
                fgets(email, sizeof(email), stdin);
                email[strcspn(email, "\n")] = 0;

                if (db_insert_user(name, age, email)) {
                    printf("User inserted successfully!\n");
                }
                break;

            case 2:
                printf("Enter user ID to update: ");
                scanf("%d", &id);
                getchar();

                printf("Enter new name: ");
                fgets(name, sizeof(name), stdin);
                name[strcspn(name, "\n")] = 0;

                printf("Enter new age: ");
                scanf("%d", &age);
                getchar();

                printf("Enter new email: ");
                fgets(email, sizeof(email), stdin);
                email[strcspn(email, "\n")] = 0;

                if (db_update_user(id, name, age, email)) {
                    printf("User updated successfully!\n");
                }
                break;

            case 3:
                printf("Enter user ID to delete: ");
                scanf("%d", &id);
                if (db_delete_user(id)) {
                    printf("User deleted successfully!\n");
                }
                break;

            case 4:
                printf("Enter user ID to get: ");
                scanf("%d", &id);
                if (db_get_user(id, &user)) {
                    printf("User found:\n");
                    printf("ID: %d\n", user.id);
                    printf("Name: %s\n", user.name);
                    printf("Age: %d\n", user.age);
                    printf("Email: %s\n", user.email);
                } else {
                    printf("User not found.\n");
                }
                break;

            case 5:
                printf("All users:\n");
                db_list_users();
                break;

            case 6:
                if (db_execute_transaction()) {
                    printf("Transaction executed successfully!\n");
                }
                break;

            case 7:
                printf("Enter search term: ");
                fgets(search_term, sizeof(search_term), stdin);
                search_term[strcspn(search_term, "\n")] = 0;
                db_search_users(search_term);
                break;

            case 0:
                printf("Exiting...\n");
                break;

            default:
                printf("Invalid choice!\n");
        }
    } while (choice != 0);

    db_close();
    return 0;
}
